package com.cfh.springcloudclientapplication.remoteevent;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.web.bind.annotation.*;


@RestController
public class RemoteAppEventSenderController implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher publisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }


    @GetMapping("/send/remote/event")
    public String send(@RequestParam("message") String message) {
        publisher.publishEvent(message);
        return "Sent";
    }

    @PostMapping("/send/remote/event/{appName}")
    public String sendAppCluster(@PathVariable String appName,
                                 @RequestBody Object data) {
        RemoteAppEvent event = new RemoteAppEvent(data, appName, true);
        publisher.publishEvent(event);
        return "Sent";
    }
}
